package asia.lxq.cic.entity;

import asia.lxq.cic.config.GlobalConfig;
import asia.lxq.cic.dao.CommentMapper;
import asia.lxq.cic.dao.UserMapper;
import asia.lxq.cic.util.UserDirUtil;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.io.File;
import java.util.Date;

@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Article {
    @TableId(value = "article_id",type = IdType.AUTO)
    int articleId;
    String articleTitle;
    String articleDesc;
    Date articleDate;
    String articleContent;
    String articleStatus;
    String articleResourceId;
    int articleAuthorId;
    String articleExtent;
    int articleCollegeId;
    int articleThumbUp;
    int articleEye;
    @TableField(exist = false)
    String authorPicUrl;
    @TableField(exist = false)
    String articleCoverUrl;
    @TableField(exist = false)
    String articleNickname;
    @TableField(exist = false)
    long commentCount=0;

    public void query(UserMapper userMapper, CommentMapper commentMapper, boolean simply){
        User user=userMapper.selectById(articleAuthorId);
        if (simply)articleContent=null;
        String userPicFilePath = UserDirUtil.getRealUserPic(user.getUserId());
        File userPicFile=new File(userPicFilePath);
        if (userPicFile.exists())authorPicUrl=UserDirUtil.getUserPic(user.getUserId());else authorPicUrl=null;
        articleNickname=user.getNickname();
        articleCoverUrl= UserDirUtil.getUserVirtualResDir(user.getUserId(),
                "article_cover_"+articleResourceId+".png");
        LambdaQueryWrapper<Comment> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Comment::getCommentArticleId,articleId);
        commentCount=commentMapper.selectCount(wrapper);
    }
}